logo
Menu
Elevating Customer Support With a Whatsapp Assistant.

Elevating Customer Support With a Whatsapp Assistant.

This app uses RAG to query Amazon Kendra and DynamoDB to responses while managing conversation memory and session time to self-service through natural language.

Elizabeth Fuentes
Amazon Employee
Published Jan 30, 2024
Last Modified Feb 2, 2024
This article was written with Guillermo Ruiz
What was an exciting trip to Las Vegas for a presentation as speaker on re:Invent 2023 turned into an unexpected journey to an unknown destination. A booking mishap resulted in the speaker gaining an airline ticket to Las Vegas, New Mexico instead of the well-known Las Vegas.
This hilarious error set the stage for investigating how advanced technologies like generative AI and Retrieval Augmented Generation(RAG) can revolutionize traditional support channel models, turning a complicated ticket change into a quick solution through fluid conversation.
This blog will guide you through building a Whatsapp Assistant Application that uses an LLM assistant. It can understand and communicate in multiple languages, both written and spoken, with the goal of providing self-service assistance through natural conversations and remembering previous interactions to solve common travel issues Capable of checking the status of passenger flights, as well as the data related to their trip, using your reservation number or passenger identification.
✅ The Whatsapp Assistant Application is ready to deploy using AWS Cloud Development Kit. Find the code in Elevating Customer Support With Rag Langchain Agent Bedrock Dynamodb And Kendra github repo.

How The Whatsapp Travel Assistant Work?

Let's break it down into three main blocks:
How The Travel Assistant Work
Fig 1. Travel assistant High-level overview

1. Message Input and Initial Processing:

Message Processing Based on Format
Fig 2. Travel assistant High-level overview"
A user sends either a text or voice message via WhatsApp, the message hits the Amazon API Gateway, then a whastapp_in AWS Lambda Function is executed to process new WhatsApp messages, extract relevant details, and write them in Amazon DynamoDB Streams.

2. Message Processing Based on Format:

Message Processing Based on Format
Fig 3. Travel assistant High-level overview
The process_stream AWS Lambda Function is trigger by events in DynamoDB Streams] and identify the format of the message (text or audio):
- If Message is Text Format: a AWS Lambda Function named API_bedrock_agents is triggered, the heart and brain of the Travel Assistant. We’ll explain it later.
- If Message is Audio Format: The star_transcibe_job Lambda Function is triggered. This Lambda Function downloads the WhatsApp audio from the link in the message in an Amazon S3 bucket, using authentication, then converts the audio to text using the Amazon Transcribe start_transcription_job API, which leaves the transcript file in an Output Amazon S3 bucket.
Function that invokes start_transcription_job looks like this:
1
2
3
4
5
6
7
8
9
10
11
def start_job_transciptor (jobName,s3Path_in,OutputKey,codec):
response = transcribe_client.start_transcription_job(
TranscriptionJobName=jobName,
IdentifyLanguage=True,
MediaFormat=codec,
Media={
'MediaFileUri': s3Path_in
},
OutputBucketName = BucketName,
OutputKey=OutputKey
)
✅ Notice that the IdentifyLanguage parameter is configured to True. Amazon Transcribe can determine the primary language in the audio.
The transcribe_done Lambda Function is triggered once the Transcribe Job is complete. It extracts the transcript from the Output S3 bucket and sends it to the agent.

3. LLM Processing and Response:

Here we explain the heart ❤️ and brain 🧠 of the Travel Assistant.
The Travel Assistant is managed by a Langchain Agent, a framework for developing LLM applications, who uses the Amazon Bedrock API to understand and respond through natural language by invoking a foundational models. This Travel Assistant employs Anthropic Claude, from which the assistant gains multilingual capabilities.
By using Retrieval Augmented Generation (RAG), the assistant can extract passenger details from an Amazon DynamoDB table and answer questions about how to resolve specific cases to a knowledge base in Amazon Kendra.
In order to have seamless conversations that recall past messages, we use the Langchain feature for memory management. This feature stores conversation content in a DynamoDB Table called SessionTable. To handle session duration, we use a DynamoDB table named user_metadata, this table stores the user’s metadata and session start, which is queried and compared with a define max session duration during each interaction. Change session duration here.
📚 Kenton Blacutt, an AWS Associate Cloud App Developer, collaborated with Langchain, creating the Amazon Dynamodb based memory class that allows us to store the history of a langchain agent in an Amazon DynamoDB.
LLM Processing and Response
Fig 4. Travel assistant agent
The query_table_passanger Lambda Function is invoked by the agent when it needs to know the passenger's information or query user_metadata in DynamoDB table.
When the agent finishes assembling the response, they respond to Whatapp through the whatsapp_out Lambda Function.

Let's Build The Travel Assistant

The Travel Assistant Diagram
The Travel Assistant Diagram


Step 0: Activate WhatsApp account Facebook Developers

Step 1: Previous Configuration

Clone the repo
1
git clone https://github.com/build-on-aws/elevating-customer-support-with-rag-langchain-agent-bedrock-dynamodb-and-kendra.git
Go to:
1
cd customer-support-bot
Create The Virtual Environment: by following the steps in the README
1
2
python3 -m venv .venv
source .venv/bin/activate
for windows:
1
.venv\Scripts\activate.bat
Install The Requirements:
1
pip install -r requirements.txt
Set Values:
In customer_support_bot_stack.py edit this line with the whatsapp Facebook Developer app number:
1
DISPLAY_PHONE_NUMBER = 'YOU-NUMBER'
This agent maintains the history of the conversation, which is stored in the session_tabble Amazon DynamoDB table, also have control session management in the session_active_tabble Amazon DynamoDB table, and sets the time here in this line:
1
if diferencia > 300: #session time in seg

Step 2: Deploy The App With CDK.

Follow steps here
Synthesize The Cloudformation Template With The Following Command:
1
cdk synth
✅ 🚀 The Deployment:
1
cdk deploy
Review what is deployed in the stack:
Go to the AWS Cloudformation console, select the region where you deployed and click on CustomerSupportBotStack:
 Amazon CloudFormation Stack
Fig 6. Amazon CloudFormation Stack
Wait a few minutes
This stack automatically creates an Amazon Kendra Index with the data source that contains the Q&A database of the airline "La inventada", you must wait a few minutes for all the data to be synchronized.
Amazon Kendra Data Sources
Fig 7. Amazon Kendra Data Sources

Step 3: Activate WhatsApp Messaging In The App

Go to AWS Secrets Manager and edit the WhatsApp settings and replace them with Facebook Developer settings.
Activate WhatsApp Messaging In The App
Fig 8. Activate WhatsApp Messaging In The App
Configure Webhook In Facebook Developer Application
Fig 9. Configure Webhook In Facebook Developer Application

Let´s try!

Q&A:
You can start asking for customer service information as if it were an airline customer service line.
Q&A Example
Q&A Example
Passenger information:
The CDK stack creates the dynamoDB table named Passenger_ID with the sample passenger dataset from Kaggle. Select one and request information regarding it.
Passanger ID
Passanger ID
What if I now change the language and ask in Spanish?
Passanger ID in Spanish
Ask Passanger ID in Spanish
The multilanguage function depends on the LLM you use.
Send it voice notes:
Send Voice Note
Send Voice Note
Amazon Transcribe can detect spoken languages in your media without requiring a language code. 🌎

🚀 Keep testing, play with the prompt in the agent Amazon Lambda function and adjust it to your need.

Conclusion

While the speaker's trip to the wrong city of Las Vegas began as a comedy of errors, it also highlighted an important opportunity to reimagine customer service.
Whatsapp Travel Assistant is the application that the speaker imagined, an application capable of delivering a self-service experience for travelers through natural conversations.
Whatsapp Travel Assistant can:
  • - Understand conversations in any language, both written and spoken, and response in the same language.
    - Query a knowledge database in Amazon Kendra and an Amazon DynamoDB Table using RAG.
    - Deliver sophisticated answers according to the query using RAG, querying knowledge databases in Amazon Kendra, and tables in Amazon DynamoDB.
    - Manage conversation memory and store it in an Amazon DynamoDB table.
    - Managing session time through a Amazon Dynamodb Table.
    We invite you to build this application, play with it, improve it and tell us how it went for you.
Thanks! 👩🏻🧔🏻‍♂️


🚀 Some links for you to continue learning and building:

Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.

Comments